﻿
-- =============================================
-- Author:		zengfei	
-- Create date: 03m/17/2012
-- Description:	更新发票主表
-- =============================================
CREATE PROCEDURE [dbo].[Usp_ModifyInvoiceMaster]
	@p_Action	char(1),
	@p_InvoiceNo	varchar(30),
	@p_CustomerNo	varchar(8),
	@p_VendorNo	varchar(8),
	@p_ProductAmount	numeric(15,2),
	@p_TaxAmount	numeric(15,2),
	@p_InvoiceDate	datetime,
	@p_Recipient	varchar(20),
	@p_Review	varchar(20),
	@p_Drawer	varchar(20),
	@p_ReferenceNo	varchar(30),
	@p_Remark	varchar(60),
	@p_InvoiceType	int,
	@p_RecordID	int out,
	@p_UserID	varchar(60),
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
begin transaction

if	@p_Action = 'A'
begin
	insert into InvoiceMaster (InvoiceNo, CustomerNo, VendorNo, ProductAmount, TaxAmount, InvoiceDate, Recipient, Review, Drawer, ReferenceNo, Remark, InvoiceType)
		values (@p_InvoiceNo, @p_CustomerNo, @p_VendorNo, @p_ProductAmount, @p_TaxAmount, @p_InvoiceDate, @p_Recipient, @p_Review, @p_Drawer, @p_ReferenceNo, @p_Remark, @p_InvoiceType)
		
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY
end
else if	@p_Action = 'C'
begin
	update InvoiceMaster set CustomerNo = @p_CustomerNo,
								VendorNo = @p_VendorNo,
								ProductAmount = @p_ProductAmount, 
								TaxAmount = @p_TaxAmount, 
								InvoiceDate = @p_InvoiceDate, 
								Recipient = @p_Recipient, 
								Review = @p_Review, 
								Drawer = @p_Drawer, 
								ReferenceNo = @p_ReferenceNo, 
								Remark = @p_Remark, 
								InvoiceType = @p_InvoiceType 
							where RecordID = @p_RecordID 
							
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end
else if	@p_Action = 'D'
begin
	delete from InvoiceMaster where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end

insert into InvoiceMaster_log (InvoiceNo, CustomerNo, VendorNo, ProductAmount, TaxAmount, InvoiceDate, Recipient, Review, Drawer, ReferenceNo, Remark, InvoiceType, UserID, ModifyTime, [Action])
	values (@p_InvoiceNo, @p_CustomerNo, @p_VendorNo, @p_ProductAmount, @p_TaxAmount, @p_InvoiceDate, @p_Recipient, @p_Review, @p_Drawer, @p_ReferenceNo, @p_Remark, @p_InvoiceType, @p_UserID, GETDATE(), @p_Action)
	
if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle

commit transaction

set @p_RFlag = 1


ErrHandle:

if	@@TRANCOUNT > 0	rollback transaction

set @p_RFlag = -1


